package com.tool.cn.util;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;


/**
 * 
 * @author wangzonghui
 * @date 2022年3月16日 下午3:24:36
 * @Description http ssl工具类
 */
public final class HttpSslUtil {
	
	private static final int timeOut = 45000;
	
	private static class TrustAnyTrustManager implements X509TrustManager {

		public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
		}

		public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
		}

		public X509Certificate[] getAcceptedIssuers() {
			return new X509Certificate[] {};
		}
	}

	private static class TrustAnyHostnameVerifier implements HostnameVerifier {
		public boolean verify(String hostname, SSLSession session) {
			return true;
		}
	}

	public static String doHttpsPost(String url, String content)throws Exception{
		return doHttpsPost(url, content, "application/json; charset=utf-8");
	}
	
	/**
	 * post方式请求服务器(https协议)
	 *
	 * @param url     请求地址
	 * @param content 参数
	 * @param application 编码 请求类型加编码 如：application/json;或指定编码 application/json;charset=utf-8
	 * @return
	 */
	public static String doHttpsPost(String url, String content,String application)throws Exception {
		String result = "";
		SSLContext sc = SSLContext.getInstance("SSL");
		sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());

		URL console = new URL(null, url, new sun.net.www.protocol.https.Handler());
		HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
		
		if(StringUtil.isNotEmpty(application)) {
			conn.setRequestProperty("Content-Type", application);
		}
		
		
		conn.setSSLSocketFactory(sc.getSocketFactory());
		conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
		conn.setDoOutput(true);
		conn.setConnectTimeout(timeOut);
		conn.setReadTimeout(timeOut);
		
		conn.connect();
		DataOutputStream out = new DataOutputStream(conn.getOutputStream());
		out.write(content.getBytes());
		// 刷新、关闭
		out.flush();
		out.close();
		
		InputStream is = conn.getInputStream();
		if (is != null) {
			ByteArrayOutputStream outStream = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = is.read(buffer)) != -1) {
				outStream.write(buffer, 0, len);
			}
			is.close();
			
			byte[] array = outStream.toByteArray();
			result = new String(array, "utf-8");
			return result;
		}
		return result;
	}

	/**
	 * get方式请求服务器(https协议)
	 *
	 * @param url     请求地址
	 * @return
	 */
	public static String doHttpsGet(String url)throws Exception {
		String result = "";
		SSLContext sc = SSLContext.getInstance("SSL");
		sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());

		URL httpUrl = new URL(null, url, new sun.net.www.protocol.https.Handler());
		HttpsURLConnection conn = (HttpsURLConnection) httpUrl.openConnection();
		conn.setRequestMethod("GET");
		conn.setSSLSocketFactory(sc.getSocketFactory());
		conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
		conn.setDoOutput(true);
		conn.setConnectTimeout(timeOut);
		conn.setReadTimeout(timeOut);

		conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
		// conn.setRequestProperty("Authorization", "xxxxxx" + token);
		conn.connect();

		InputStream is = conn.getInputStream();
		if (is != null) {
			ByteArrayOutputStream outStream = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = is.read(buffer)) != -1) {
				outStream.write(buffer, 0, len);
			}
			is.close();

			byte[] array = outStream.toByteArray();
			result = new String(array, "utf-8");
			return result;
		}

		return result;
	}
}
